package com.liyu.rabbit;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * 作者: 李誉
 * 时间: 2022-03-03 10:40
 * 描述: 消费者
 */
public class Consumer {

    // 消费消息
    public static void main(String[] args) throws IOException, TimeoutException {
        // 1、创建连接工厂
        ConnectionFactory connectionFactory = new ConnectionFactory();
        // 2、设置连接信息
        connectionFactory.setHost("127.0.0.1");
        connectionFactory.setPort(5672);
        connectionFactory.setUsername("guest");
        connectionFactory.setPassword("guest");


        // 3、创建连接
        Connection connection = connectionFactory.newConnection();
        // 4、获得信道
        Channel channel = connection.createChannel();

        // 5、发送消息
        channel.basicConsume(Producer.QUEUE_NAME, true, (consumerTag, message) -> {
            System.out.println("消费一条消息成功：" + new String(message.getBody()));
        }, (consumerTag) -> {
            System.out.println("消费一条消息失败：");
        });



    }
}
